package com.creditease.shield.flow;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.creditease.shield.flow.cache.CacheContainer;
import com.creditease.shield.flow.dao.KeyBankDao;
import com.creditease.shield.flow.pojo.KeyBankPO;

/**
 * Created by xiaoleizhao on 2017/9/19.
 */
@SpringBootApplication
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableScheduling
public class ServerApplication {
	private static final Logger log = LoggerFactory.getLogger(ServerApplication.class);
	
    public static void main(String[] args) throws Exception {
    	
    	ApplicationContext app =SpringApplication.run(ServerApplication.class, args);
        KeyBankDao repository = (KeyBankDao)app.getBean("keyBankDao");
		
		List<KeyBankPO> keyList= repository.findAllKeyBank(); 
		//cache key bank
		cacheInitData(keyList);
		
    }
        
    private static void cacheInitData(List<KeyBankPO> keyList){
		
		CacheContainer cache = CacheContainer.getInstance(); 
		cache.setCache("flowNoCache");
		
		String keyName = null; 
		for(KeyBankPO basekey: keyList){
			keyName = basekey.getKeyName();
			cache.put("flowNoCache",keyName, basekey);
			log.info("Cache BaseKey into flowNoCache,key:"+ keyName + ",Value:"+ basekey.toString());
			
			cache.put("flowNoCache","0"+keyName, basekey.getKeyValue());
			log.info("Cache keyInitValue into flowNoCache,key:" + keyName + ",Value:"+ basekey.getKeyValue());
			
			cache.put("flowNoCache","1"+keyName, basekey.getKeyValue());
			log.info("Cache keyRefValue into flowNoCache,key:" + keyName + ",Value:"+ basekey.getKeyValue());
		}
	}
}